package com.hilaver.dzmis.dao;

import java.util.List;

import org.hibernate.Query;

import com.hilaver.dzmis.basicinfo.BiAccessoryButton;
import com.hilaver.dzmis.basicinfo.BiAccessoryOthers;
import com.hilaver.dzmis.basicinfo.BiAccessoryZip;
import com.hilaver.dzmis.basicinfo.BiDZColor;

public class StockDAO extends BaseHibernateDAO {
	
	public List getYarnAllPagination(int offset, int limit,
			String sort, String order, String[] filters) throws Exception {
		try {
			String whereStatement = getWhereStatement(filters);
			String queryString = "from " + BiDZColor.class.getName() + whereStatement + 
					("".equals(whereStatement)?" where ":" and ") +
			        " (inStock<>0 or onTheWayStock<>0 or inOutSum<>0 or bookedStock<>0 or outStock<>0) " +
					" order by " + sort
					+ " " + order;
			Query queryObject = getSession().createQuery(queryString);
			queryObject.setFirstResult(offset);
			queryObject.setMaxResults(limit);
			return queryObject.list();
		} catch (Exception e) {
			e.printStackTrace();
			throw e;
		}
	}
	
	public int getYarnCount(String[] filters) throws Exception{
		try {
			String whereStatement = getWhereStatement(filters);
			String queryString = "select count(*) from " + BiDZColor.class.getName() + whereStatement +
				("".equals(whereStatement)?" where ":" and ") +
		        " (inStock<>0 or onTheWayStock<>0 or inOutSum<>0 or bookedStock<>0 or outStock<>0) ";
			Query queryObject = getSession().createQuery(queryString);
			return ((Integer) queryObject.iterate().next()).intValue();
		} catch (Exception e) {
			e.printStackTrace();
			throw e;
		}
	}
	
	public List getZipAllPagination(int offset, int limit,
			String sort, String order, String[] filters) throws Exception {
		try {
			String whereStatement = getWhereStatement(filters);
			String queryString = "from " + BiAccessoryZip.class.getName() + whereStatement + 
					("".equals(whereStatement)?" where ":" and ") +
			        " (inStock<>0 or onTheWayStock<>0 or inOutSum<>0 or bookedStock<>0 or outStock<>0) " +
					" order by " + sort
					+ " " + order;
			Query queryObject = getSession().createQuery(queryString);
			queryObject.setFirstResult(offset);
			queryObject.setMaxResults(limit);
			return queryObject.list();
		} catch (Exception e) {
			e.printStackTrace();
			throw e;
		}
	}
	
	public int getZipCount(String[] filters) throws Exception{
		try {
			String whereStatement = getWhereStatement(filters);
			String queryString = "select count(*) from " + BiAccessoryZip.class.getName() + whereStatement +
				("".equals(whereStatement)?" where ":" and ") +
		        " (inStock<>0 or onTheWayStock<>0 or inOutSum<>0 or bookedStock<>0 or outStock<>0) ";
			Query queryObject = getSession().createQuery(queryString);
			return ((Integer) queryObject.iterate().next()).intValue();
		} catch (Exception e) {
			e.printStackTrace();
			throw e;
		}
	}
	
	public List getButtonAllPagination(int offset, int limit,
			String sort, String order, String[] filters) throws Exception {
		try {
			String whereStatement = getWhereStatement(filters);
			String queryString = "from " + BiAccessoryButton.class.getName() + whereStatement + 
					("".equals(whereStatement)?" where ":" and ") +
			        " (inStock<>0 or onTheWayStock<>0 or inOutSum<>0 or bookedStock<>0 or outStock<>0) " +
					" order by " + sort
					+ " " + order;
			Query queryObject = getSession().createQuery(queryString);
			queryObject.setFirstResult(offset);
			queryObject.setMaxResults(limit);
			return queryObject.list();
		} catch (Exception e) {
			e.printStackTrace();
			throw e;
		}
	}
	
	public int getButtonCount(String[] filters) throws Exception{
		try {
			String whereStatement = getWhereStatement(filters);
			String queryString = "select count(*) from " + BiAccessoryButton.class.getName() + whereStatement +
				("".equals(whereStatement)?" where ":" and ") +
		        " (inStock<>0 or onTheWayStock<>0 or inOutSum<>0 or bookedStock<>0 or outStock<>0) ";
			Query queryObject = getSession().createQuery(queryString);
			return ((Integer) queryObject.iterate().next()).intValue();
		} catch (Exception e) {
			e.printStackTrace();
			throw e;
		}
	}
	
	public List getOthersAllPagination(int offset, int limit,
			String sort, String order, String[] filters) throws Exception {
		try {
			String whereStatement = getWhereStatement(filters);
			String queryString = "from " + BiAccessoryOthers.class.getName() + whereStatement + 
					("".equals(whereStatement)?" where ":" and ") +
			        " (inStock<>0 or onTheWayStock<>0 or inOutSum<>0 or bookedStock<>0 or outStock<>0) " +
					" order by " + sort
					+ " " + order;
			Query queryObject = getSession().createQuery(queryString);
			queryObject.setFirstResult(offset);
			queryObject.setMaxResults(limit);
			return queryObject.list();
		} catch (Exception e) {
			e.printStackTrace();
			throw e;
		}
	}
	
	public int getOthersCount(String[] filters) throws Exception{
		try {
			String whereStatement = getWhereStatement(filters);
			String queryString = "select count(*) from " + BiAccessoryOthers.class.getName() + whereStatement +
				("".equals(whereStatement)?" where ":" and ") +
		        " (inStock<>0 or onTheWayStock<>0 or inOutSum<>0 or bookedStock<>0 or outStock<>0) ";
			Query queryObject = getSession().createQuery(queryString);
			return ((Integer) queryObject.iterate().next()).intValue();
		} catch (Exception e) {
			e.printStackTrace();
			throw e;
		}
	}
}
